//
//  ST_TalkCupidVideoView.swift
//  btvideo
//
//  Created by Jason on 2021/7/26.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_TalkCupidVideoView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        addSubview(playerView)
        playerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        addSubview(chat_btn)
        chat_btn.snp.makeConstraints { (make) in
            make.height.equalTo(34)
            make.width.equalTo(138)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10)
            make.height.equalTo(18)
            make.left.equalToSuperview().offset(15)
            make.right.equalTo(chat_btn.snp.left).offset(-5)
        }
        
        addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(avatar_image_view_height)
            make.bottom.equalTo(content_label.snp.top).offset(-10)
            make.left.equalTo(content_label)
        }
        
        addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.top.equalToSuperview().offset(15-(44-18)/2.0)
            make.right.equalToSuperview().offset(-15+(44-18)/2.0)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //    MARK:- Properties
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    
    fileprivate lazy var playerView:ST_PeculiarityVideoPlayView = {
        let control = ST_PeculiarityVideoPlayView()
        control.isUserInteractionEnabled = false
        control.backgroundColor = .clear
        control.clipsToBounds = true
        control.isMuted = true
        control.loopPlay = true
        
        return control
    }()
    
    fileprivate(set) lazy var close_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("注册_icon_关闭")!)
//        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
//        control.text = "TA的丘比特之箭射中了你哟！"
        control.text = "缘分恋爱"
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value: 16), weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "     "
        control.textColor = .white
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    
    fileprivate(set) lazy var chat_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = #colorLiteral(red: 0.9960784314, green: 0.7607843137, blue: 0.02745098039, alpha: 1)
        control.cornerRadius = 34/2.0
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
//        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var avatar_image_view_height = 55/375.0*ScreenWidth
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = avatar_image_view_height/2.0
        control.borderColor = UIColor.white
        control.borderWidth = 1
        
        return control
    }()
    
    func bind_data(json:JSON,type:TalkCupidViewType){
        
        let herId = json["accid"].stringValue
        
        
        var avatar = json["avatar_" + herId].stringValue
        
        if avatar.count < 2{
            avatar = json["avatar"].stringValue
        }
        
        
        
        avatar_image_view.pin_setImage(from: avatar.compress_small_image(width: 300).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        cover_image_view.pin_setImage(from: json["url"].stringValue.compress_small_image(width: 300*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        var content = ""
        switch type {
        case .recommendSomeOne:
            
            content = json["age"].stringValue + "岁 " + json["address"].stringValue + " 距离" + (json["distance"].stringValue.contains("km") ? json["distance"].stringValue : json["distance"].stringValue + "km")
            break
        case .matchedConversation:
            content = (json["distance"].stringValue.contains("km") ? json["distance"].stringValue : json["distance"].stringValue + "km") + " 在线"
            break
        default:
            content = (json["distance"].stringValue.contains("km") ? json["distance"].stringValue : json["distance"].stringValue + "km") + " 在线"
        }
        
        content_label.text = content
            
        if json["type"].stringValue == "1"{
            chat_btn.setTitleForAllStates("立即去语音聊天")
        }else{
            chat_btn.setTitleForAllStates("立即去视频聊天")
        }
        
        if let videoUrl = json["video_url"].stringValue.url{
            play(url: videoUrl)
        }
    }
    
    
                                                        
    fileprivate func play(url:URL){
            
        pause_video()
        
        playerView.play(url: url)
        
    }
    
    func pause_video(){
      
        playerView.stop()
        
    }
    
    deinit {
        pause_video()
    }
    
}
